package com.fqgj.sentry.biz.mc;

import com.fqgj.sentry.common.exception.BizException;
import com.fqgj.sentry.common.exception.enums.ErrorCodeEnums;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.manage.entity.McAlarmManEntity;
import com.fqgj.sentry.manage.service.ConfigService;
import com.fqgj.sentry.biz.mc.domain.AddAlarmManInfo;
import com.fqgj.sentry.biz.mc.domain.ModifyAlarmManInfo;
import com.fqgj.sentry.biz.mc.vo.config.AlarmManVo;
import com.github.pagehelper.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-07-16 下午12:08
 */

@Component
public class ConfigBusiness {
    @Autowired
    private ConfigService configService;

    public Long addAlarmMan(AddAlarmManInfo alarmManInfo) {
        McAlarmManEntity alarmManEntity = new McAlarmManEntity();
        BeanUtils.copyProperties(alarmManInfo,alarmManEntity);
        McAlarmManEntity entity = configService.insertAlarmMan(alarmManEntity);
        return entity.getId();
    }

    public Boolean delAlarmMan(Long merchantId, Long alarmManId) {
        return configService.deleteAlarmMan(merchantId, alarmManId);
    }

    public Boolean modifyAlarmMan(ModifyAlarmManInfo manInfo) {
        McAlarmManEntity entity = configService.selectAlarmMan(manInfo.getManId());
        if (!entity.getMerchantId().equals(manInfo.getMerchantId())) {
            throw new BizException(ErrorCodeEnums.merchant_alarm_man);
        }
        entity.setEmail(manInfo.getEmail())
                .setMobile(manInfo.getMobile())
                .setName(manInfo.getName())
                .setRemarks(manInfo.getRemarks())
                .setGmtModified(new Date());
        configService.updateAlarmMan(entity);
        return true;
    }

    public ApiPage queryAlarmManForPage(Long merchantId, String name, String email, Integer pageNum, Integer pageSize) {
        Page<McAlarmManEntity> page = configService.selectAlarmManForPage(merchantId, name, email, pageNum, pageSize);
        List<AlarmManVo> alarmManVoList = new ArrayList<>();
        for (McAlarmManEntity entity : page.getResult()) {
            AlarmManVo alarmManVo = new AlarmManVo()
                    .setAlarmManId(entity.getId())
                    .setName(entity.getName())
                    .setEmail(entity.getEmail())
                    .setRemarks(entity.getRemarks())
                    .setMobile(entity.getMobile())
                    .setGmtModified(entity.getGmtModified());
            alarmManVoList.add(alarmManVo);
        }
        ApiPage apiPage = new ApiPage();
        apiPage.setPageNum(pageNum)
                .setPageSize(pageSize)
                .setTotal(page.getTotal())
                .setData(alarmManVoList);
        return apiPage;
    }


}
